// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
//ts实现控制Monster的跳动
// const compare = require("compare")
const {ccclass, property} = cc._decorator;
@ccclass
export default class compare extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';


    /**
     * Monster移动数据
     * Jump Height: 150
       Jump Duration: 0.3
       Max Move Speed: 400
       Accel: 1000
     */

    @property
    jumpHeight:number = 0;
    @property
    jumpTime:number = 0;
    @property
    setSpeed:number = 0;//设置速度
    @property
    maxSpeed:number = 0;//最大速度
    
    @property
    moveRight:boolean = false;
    @property
    moveLeft:boolean = false;

    @property
    jumpX:number = 0;
    @property
    jumpY:number = 0;

    // @property
    // component = null;

    private masterAction() {

        var monsterUp = cc.tween(this.node).by(this.jumpTime,{y:this.jumpHeight},{easing:'sineIn'});
        var monsterDown = cc.tween(this.node).by(this.jumpTime,{y:-this.jumpHeight},{easing:'sinOut'});
        var totalAction = cc.tween().sequence(monsterUp,monsterDown);
        return cc.tween().repeat(this.setSpeed,totalAction);

    }

    start () {
        //compare 添加组件
        // this.addComponent("compare");
    }

    private boardKeyUp(event) {
        cc.log("boardKeyUp");

        cc.log("event.keyCode对应键盘的按键",event.keyCode);
        switch(event.keyCode) {
            case cc.macro.KEY.left:
                this.moveLeft = true;
                break;

            case cc.macro.KEY.right:
                this.moveRight = true;
                break;
        }
    }

    private boardKeyDown(event) {
        cc.log("boardKeyDown");

        switch(event.keyCode) {
            case cc.macro.KEY.left:
                this.moveLeft = false;
                break;

            case cc.macro.KEY.right:
                this.moveRight = false;
                break;
        }

    }

    onLoad() {
        var tweenStart = this.masterAction();
        cc.tween(this.node).then(tweenStart).start();

        this.jumpX = 0 

        this.moveLeft = false;
        this.moveRight = false;

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.boardKeyDown,this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.boardKeyUp,this);

    }

    //update 做更新
    update (dt) {

        var jumpSpeed = 10;

        //左右两边跳动;
        if(this.moveLeft) {
            this.jumpX-= jumpSpeed * dt;
        } else if(this.moveRight)  {
            this.jumpX+= jumpSpeed * dt;
        }

        //对怪兽的速度进行限制;
        if(Math.abs(this.jumpX) > this.maxSpeed) {
            this.jumpX = this.maxSpeed * this.jumpX / Math.abs(this.jumpX);
        }
       
        this.node.x += this.jumpX * dt;
    }
}
